<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>安全补丁管理</title>
    <link rel="stylesheet" href="/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="/layui/layuiAdmin/css/admin.css" media="all">
</head>
<body>

<div class="layui-fluid">
    <div class="layui-card">
        <!-- 修改搜索表单部分 -->
        <div class="layui-form layui-card-header layuiadmin-card-header-auto" style="border-radius: 0;border-top: 4px solid #d2d2d2;">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">搜索条件</label>
                    <div class="layui-input-inline">
                        <select name="searchType">
                            <option value="macAddress">MAC地址</option>
                            <option value="hostName">主机名</option>
                            <option value="hotfixId">补丁ID</option>
                        </select>
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">关键词</label>
                    <div class="layui-input-inline">
                        <input type="text" name="keywords" placeholder="请输入搜索内容" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <button class="layui-btn layuiadmin-btn-useradmin" lay-submit lay-filter="LAY-sysuser-front-search">
                        <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                    </button>
                </div>
            </div>
        </div>

        <div class="layui-card-body">
            <!-- 表格数据加载 -->
            <table id="risk-hotfix-manage" lay-filter="risk-hotfix-manage"></table>

            <!-- 操作按钮模板 -->
            <script type="text/html" id="table-useradmin-webuser">
                <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="detail"><i class="layui-icon layui-icon-set"></i>详情</a>
            </script>

            <!-- CVE列表模板 -->
            <script type="text/html" id="cveListTpl">
                {{# if(d.cveList && d.cveList.length > 0) { }}
                <a lay-event="showCveList" style="color: #CF1900; cursor: pointer;">查看漏洞({{ d.cveList.length }})</a>
                {{# } else { }}
                无
                {{# } }}
            </script>
        </div>
    </div>
</div>

<script src="/layui/layui.js"></script>
<script type="text/javascript">
    // 日期格式转换函数
    function formatDate(str) {
        if(str != null){
            var now = new Date(str);
            var year = now.getFullYear();
            var month = now.getMonth() + 1;
            var date = now.getDate();
            var hours = now.getHours();
            var minutes = now.getMinutes();
            var seconds = now.getSeconds();
            return year + "-" + month + "-" + date + " " +
                ((hours>9)?hours:"0"+hours) + ":" +
                ((minutes>9)?minutes:"0"+minutes) + ":" +
                ((seconds>9)?seconds:"0"+seconds);
        }
        return "";
    }
</script>
<script type="text/javascript">
    layui.config({
        base: '/layui/layuiAdmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['layer','table','form'], function(){
        var $ = layui.$,
            form = layui.form,
            layer = layui.layer,
            table = layui.table;

        // 初始化表格
        var tableIns = table.render({
            elem: '#risk-hotfix-manage',
            method: 'post',
            width: 'auto',
            cellMinWidth: 80,
            title: '安全补丁数据表',
            url: '/hotfix/list',
            headers: {
                'Authorization': localStorage.getItem('token')
            },
            where: {
                page: 1,
                limit: 15
            },
            cols: [
                [
                    {field: 'id', title: 'ID', width: 80, sort: true},
                    {field: 'macAddress', title: 'MAC地址', width: 180},
                    {field: 'hotfixId', title: '补丁ID', width: 160},
                    {field: 'detectTime', title: '检测时间', width: 180, templet: function(d){
                            return formatDate(d.detectTime);
                        }},
                    {field: 'cveList', title: '关联漏洞', width: 160, templet: '#cveListTpl'},
                    {fixed: 'right', title:'操作', toolbar: '#table-useradmin-webuser', width: 100}
                ]
            ],
            page: {
                curr: 1,
                limit: 15,
                limits: [10, 15, 20, 25, 30]
            },
            done: function(res, curr, count){
                console.log('表格数据加载完成:', res);
            }
        });

        // 监听搜索
        form.on('submit(LAY-sysuser-front-search)', function(data){
            var field = data.field;
            var searchParams = {
                page: 1,
                limit: 15
            };

            if (field.searchType && field.keywords) {
                searchParams[field.searchType] = field.keywords;
            }

            tableIns.reload({
                where: searchParams,
                page: {
                    curr: 1
                }
            });
        });

        // 监听分页
        table.on('page(risk-hotfix-manage)', function(obj){
            var curr = obj.curr;
            var limit = obj.limit;

            var where = tableIns.config.where || {};
            where.page = curr;
            where.limit = limit;

            tableIns.reload({
                where: where
            });
        });
        // 监听行工具事件
        // 在表格监听工具事件中修改showCveList的处理逻辑
        table.on('tool(risk-hotfix-manage)', function(obj){
            var data = obj.data;
            console.log('当前行数据:', data); // 添加调试日志

            if(obj.event === 'showCveList' && data.cveList && data.cveList.length > 0){
                // 确保 hotfixId 存在
                if (!data.hotfixId) {
                    layer.msg('补丁ID不存在', {icon: 2});
                    return;
                }

                console.log('传递的hotfixId:', data.hotfixId);
                layer.open({
                    type: 2,
                    title: 'CVE漏洞详情 - '+data.hotfixId,
                    content: '/page/host/cveDetail?hotfixId='+encodeURIComponent(data.hotfixId),
                    area: ['1000px', '700px']
                });

            }
        });



    });
</script>

</body>
</html>
